package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryOrder;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderSettingListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 预约管理
 */
@RestController
@RequestMapping("/ordersettinglist")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService orderSettingListService;

    /**
     * 分页查询预约管理
     */
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('ORDER_QUERY')")
    public PageResult findPage(@RequestBody QueryOrder queryOrder) throws Exception {
        return orderSettingListService.findPage(queryOrder);
    }

    /**
     *编辑预约状态
     */
    @RequestMapping("/changeStatus.do")
    @PreAuthorize("hasAuthority('ORDER_EDIT')")
    public Result changeStatus(@RequestBody Map map){
        try {
            orderSettingListService.changeStatus(map);
            return new Result(true, "预约编辑状态成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑预约状态失败");
        }
    }

    /**
     *查询所有套餐
     */
    @RequestMapping("/findAll.do")
    @PreAuthorize("hasAuthority('ORDER_QUERY')")
    public Result findAll(){
        try {
            List<Setmeal> list = orderSettingListService.findAll();
            return new Result(true, "套餐查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "套餐查询失败");
        }
    }

    /**
     *添加预约
     */
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('ORDER_ADD')")
    public Result add(@RequestBody Map map){
//        System.out.println(map.get("name"));
//        System.out.println(map.get("phoneNumber"));
//        System.out.println(map.get("orderDate"));
////        System.out.println(map.get("setmealIds"));
//        System.out.println(map.get());
        try{
            Result result = orderSettingListService.add(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "添加预约失败");
        }

    }

    /**
     *根据id查询套餐以及会员信息套餐信息
     */
    @RequestMapping("/findById.do")
    public Result findById(int id){
        try{
            List<Integer> setmealIds = orderSettingListService.findById(id);
            return new Result(true, "查询预约信息成功", setmealIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询预约信息失败");
        }
    }


    /**
     * 编辑预约信息
     */
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('ORDER_EDIT')")
    public Result edit(@RequestBody Map map){
        try{
            orderSettingListService.edit(map);
            return new Result(true, "编辑预约信息成功");
        }catch (Exception e){
            return new Result(false, "编辑预约信息失败");
        }
    }

    /**
     *查询预约信息中所有的地址信息
     */
    @RequestMapping("/findAllAddress.do")
    public Result findAllAddress(){
        try {
            List<Address> list = orderSettingListService.findAllAddress();
            return new Result(true, "套餐地址成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "套餐地址失败");
        }
    }

    /**
     *根据id查询地址 用于编辑地址选中
     */
    @RequestMapping("/findAddressIdByOrderId.do")
    public Result findAddressIdByOrderId(int id){
        try{
            List<Integer> addressId = orderSettingListService.findAddressIdByOrderId(id);
            if(addressId == null){
                addressId = new ArrayList<>();
            }
            return new Result(true, "查询预约地址成功",addressId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询预约地址失败");
        }
    }

    /**
     *删除预约
     */
    @RequestMapping("/delete.do")
    @PreAuthorize("hasAuthority('ORDER_DELETE')")
    public Result delete(int id){
        try{
            orderSettingListService.delete(id);
            return new Result(true, "删除预约成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "删除预约失败");
        }
    }
}
